﻿package com.game.ammo {
	import com.game.units.Enemy;
	import com.game.utils.TowerType;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	public class Ammo {
		/* amount of time until the bullet reaches the target- "fly time" */
		private var _hit:Boolean = false;
		protected var p1:Point;
		protected var p2:Point;
		public var readyForRemoval:Boolean = false;
		protected var _angle:Number;
		/* amount of time until the wave is faded - "affected zone time" */
		protected var ammoSpeed:Number = 0.5;
		protected var destination:Number = 0;
		public var type:TowerType;
		protected var _damage:Number;
		protected var upgrades:int;
		public var depth:int = 0;
		protected var _suggestiveDistance:int = 0;
		protected var x:Number = 0;
		protected var y:Number = 0;
		protected var _point:Point = new Point();
		protected var _rect:Rectangle = new Rectangle(0, 0, 100, 100);
		public function Ammo(bulX:Number, bulY:Number, ang:Number, destination:Number, type:TowerType, upgrades:int, suggestiveDistance:int) {
			this.upgrades = upgrades;
			this.destination = destination;
			this.type = type;
			_angle = ang;
			_suggestiveDistance = suggestiveDistance;
			x = bulX;
			y = bulY;
			p1 = new Point(0, 0);
			p2 = new Point(-destination * Math.cos(_angle), -destination * Math.sin(_angle));
			initialSet();
		}
		public function clear():void {
			p1 = null;
			p2 = null;
			_point=null;
			_rect=null;
		}
		protected function initialSet():void {
		}
		public function move():void {
		}
		public function isHit(p3:Enemy):Boolean {
			return false;
		}
		public function set hit(value:Boolean):void {
			_hit = value;
			hitAction();
		}
		protected function hitAction():void {
		}
		public function get hit():Boolean {
			return _hit;
		}
		protected function pointInsideCircleDistance(p3:Enemy):Number {
			var p1:Point = new Point(x, y);
			return (p1.x - p3.x) * (p1.x - p3.x) + (p1.y - p3.y) * (p1.y - p3.y);
		}
		protected function pointToLineDistance(p3:Enemy):Number {
			//get the normalized vector of the line
			var p1:Point = new Point(x, y);
			var p2:Point = new Point(x + this.p2.x, y + this.p2.y);
			var xDelta:Number = p2.x - p1.x;
			var yDelta:Number = p2.y - p1.y;
			if ((xDelta == 0) && (yDelta == 0)) {
				// p1 and p2 cannot be the same point
				p2.x += 1;
				p2.y += 1;
				xDelta = 1;
				yDelta = 1;
			}
			var u:Number = ((p3.x - p1.x) * xDelta + (p3.y - p1.y) * yDelta) / (xDelta * xDelta + yDelta * yDelta);
			var closestPoint:Point;
			if (u < 0) {
				closestPoint = new Point(p1.x, p1.y);
			} else if (u > 1) {
				closestPoint = new Point(p2.x, p2.y);
			} else {
				closestPoint = new Point(p1.x + u * xDelta, p1.y + u * yDelta);
			}
			return Point.distance(closestPoint, new Point(p3.x, p3.y));
		}
		protected function pointToPointDistance(p3:Enemy, currentPoint:Point):Number {
			return Point.distance(currentPoint, new Point(p3.x, p3.y));
		}
		public function get damage():Number {
			return _damage;
		}
		public function get point():Point {
			return _point;
		}
		public function get rect():Rectangle {
			return _rect;
		}
	}
}